<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in">
      <div class="cornerComment-in-title">
        <span>添加/编辑招聘</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form :model="editOption" label-width="120px" ref="editForm">
          <div class="resruitment-title">岗位基本信息</div>
          <el-row>
            <el-col :span="12">
              <el-form-item label="岗位名称：" prop="name">
                <el-select v-model="editOption.job_id" placeholder="请选择">
                  <el-option
                      v-for="item in jobList"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="招聘截止时间：" prop="name">
                <el-date-picker type="date"
                                placeholder="选择日期"
                                v-model="editOption.end_time"
                                :picker-options="pickerOptions1"
                                value-format="yyyy-MM-dd"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="岗位性质：" prop="warming">
                <el-radio-group v-model="requirements.typeJob">
                  <el-radio :label="1">全职</el-radio>
                  <el-radio :label="2">兼职</el-radio>
                  <el-radio :label="3">实习</el-radio>
                  <el-radio :label="4">不限</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="招聘人数：" prop="unit">
                <el-input v-model="editOption.number" auto-complete="off" placeholder="招聘人数"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="薪资待遇：" prop="phone">
                <el-input v-model="editOption.treatment" auto-complete="off" placeholder="薪资待遇（例：5k-7k）"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="试用期限：" prop="number">
                <el-input v-model="editOption.trial_time" auto-complete="off" placeholder="试用期限（例：3月）"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="岗位描述：" prop="number">
                <el-input v-model="requirements.describe" auto-complete="off" placeholder="岗位描述" type="textarea"
                          :autosize="{ minRows:4}"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <div class="resruitment-title">岗位要求</div>
          <el-row>
            <el-col :span="12">
              <el-form-item label="学历要求：" prop="phone">
                <el-select v-model="editOption.education" placeholder="选择学历">
                  <el-option
                      v-for="item in educationList"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="岗位经验：" prop="number">
                <el-select v-model="starYear" placeholder="请选择" class="w-100">
                  <el-option
                      v-for="item in yearOptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                  </el-option>
                </el-select>
                -
                <el-select v-model="endYear" placeholder="请选择" class="w-100">
                  <el-option
                      v-for="item in yearOptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                  </el-option>
                </el-select>
                年（不填即为不限）
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="年龄：" prop="phone">
                <el-input v-model="editOption.age" auto-complete="off" placeholder="年龄/以下"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="性别：" prop="phone">
                <el-radio-group v-model="editOption.sex">
                  <el-radio :label="0">不限</el-radio>
                  <el-radio :label="1">男</el-radio>
                  <el-radio :label="2">女</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="个人信用证明：" prop="phone">
                <el-radio-group v-model="requirements.credit">
                  <el-radio :label="1">需要</el-radio>
                  <el-radio :label="0">不需要</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
            </el-col>
          </el-row>
          <div class="resruitment-title">岗位审核</div>
          <el-row>
            <el-col :span="24">
              <el-form-item label="简历审核人：" prop="phone">
                <el-button @click.native="getMoreTeach(1)" size="small">选择简历审核人（单选）</el-button>
                <el-tag
                    closable
                    :disable-transitions="false"
                    @close="handleCloseExamine()"
                    class="mr-10"
                    v-if="examine_user.realname"
                >{{ examine_user.realname }}
                </el-tag>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="面试考核人：" prop="phone">
                <el-button @click.native="getMoreTeach(2)" size="small">选择面试考核人（多选）</el-button>
                <el-tag
                    :key="tag.id"
                    v-for="(tag,index) in dynamicTags_interview"
                    closable
                    :disable-transitions="false"
                    @close="handleClose('interview',index)"
                    class="mr-10"
                >{{ tag.realname }}
                </el-tag>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="录用审核人：" prop="phone">
                <el-button @click.native="getMoreTeach(3)" size="small">选择录用审核人（多选）</el-button>
                <el-tag
                    :key="tag.id"
                    v-for="(tag,index) in dynamicTags_employ"
                    closable
                    :disable-transitions="false"
                    @close="handleClose('employ',index)"
                    class="mr-10"
                >{{ tag.realname }}
                </el-tag>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click.native="closePOpup" size="small">取消</el-button>
        <el-button @click="submitFromFn" size="small" type="primary">确认并发布招聘</el-button>
      </div>
    </div>
    <!--    单选教职工审核人-->
    <singleTeacherModle :visible.sync="singleTeachVisible" @studentFn="studentFn"
                        :dynamicTagsArray="examine_user"></singleTeacherModle>
    <!--    多选教职工审核人-->
    <MultipleTeacherModle :visible.sync="moreTeachVisible" @studentFn="studentFn"
                          :dynamicTagsArray="dynamicTagsArray"></MultipleTeacherModle>
  </div>
</template>

<script>
export default {
  props: ['visible', 'resumeId'],
  components: {},
  data() {
    return {
      dialogVisible: false,
      editOption: {
        job_id: '',// 	字符串 	必填 	1 	- 	招聘岗位id
        number: '',// 	字符串 	必填 	1 	- 	招聘人数
        treatment: '',// 	字符串 	必填 	1 	- 	待遇
        trial_time: '',//	字符串 	必填 	1 	- 	使用期限
        end_time: '',// 	字符串 	必填 	1 	- 	结束时间
        sex: 0,// 	字符串 	必填 	1 	- 	性别0不限1男2女
        age: '',// 	字符串 	必填 	1 	- 	年龄（以上）
        education: '',//	字符串 	必填 	1 	- 	学历id
        experience: '',//	字符串 	必填 	1 	- 	工作经验
        requirements: '',//	字符串 	必填 	1 	- 	岗位要求json
        recruit_examine_user: '',//	字符串 	必填 	1 	- 	简历审核人
        interview_user: '',//	字符串 	必填 	1 	- 	面试考核人员id多选逗号隔开
        employ_user: '',//	字符串 	必填 	1 	- 	录用考核人员id多选逗号隔开
      }, // 编辑界面数据
      yearOptions: [{
        value: '1',
        label: '1'
      }, {
        value: '2',
        label: '2'
      }, {
        value: '3',
        label: '3'
      }, {
        value: '4',
        label: '4'
      }, {
        value: '5',
        label: '5'
      }, {
        value: '6',
        label: '6'
      }, {
        value: '7',
        label: '7'
      }, {
        value: '8',
        label: '8'
      }, {
        value: '9',
        label: '9'
      }, {
        value: '10',
        label: '10'
      }, {
        value: '11',
        label: '11'
      }, {
        value: '12',
        label: '12'
      }],
      // 限制时间选择
      pickerOptions1:
          {
            disabledDate(time) {
              return time.getTime() < Date.now() // - 8.64e7
            }
          },
      starYear: '',
      endYear: '',
      requirements: {
        credit: 1, // 征信
        typeJob: 1, // 工作性质
        describe: ''
      },
      jobList: [], // 岗位管理
      educationList: [], // 学历
      describe: '',
      // 多选教职工
      singleTeachVisible: false,
      moreTeachType: 0,// 选择的类型 1 简历审核 2 面试 3 录用
      moreTeachVisible: false,
      examine_user_tag: [],
      examine_user: {}, // 审核人
      dynamicTagsArray_interview: {},// 面试考核人员
      dynamicTags_interview: [],// 面试考核人员
      dynamicTagsId_interview: [],// 面试考核人员
      dynamicTagsArray_employ: {},// 录用考核人员
      dynamicTags_employ: [],// 录用考核人员
      dynamicTagsId_employ: [],// 录用考核人员
      dynamicTagsArray: {},
      dynamicTags: [],
      dynamicTagsId: [] // 选中的id，进行提交
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        if (this.resumeId) {
          this.InitFn()
        }
        // 获取岗位信息
        this.JobFn()
        this.EducationFn()
      } else {
        this.clearFrom()
      }
    }
  },
  created() {

  },
  methods: {
    submitFromFn(type, option) {
      console.log(this.editOption)
      let dataOption = this.getJson(this.editOption)
      // 参数判断
      if (!this.editOption.job_id) {
        this.$message.error('选择招聘岗位')
        return
      }
      // 判断时间不能小于今天
      let d = new Date(Date.parse(this.editOption.end_time.replace(/-/g, "/")));
      let curDate = new Date();
      if (d <= curDate) {
        this.$message.error('招聘时间需大于今天')
        return
      }

      if (!this.editOption.number) {
        this.$message.error('拟招聘人数不能为空')
        return
      }
      if (!this.editOption.treatment) {
        this.$message.error('薪资待遇不能为空')
        return
      }
      if (!this.editOption.trial_time) {
        this.$message.error('试用期限不能为空')
        return
      }
      if (!this.editOption.education) {
        this.$message.error('学历要求不能为空')
        return
      }
      // 岗位经验验证       starYear endYear
      if ((this.starYear && !this.endYear) || (!this.starYear && this.endYear)) {
        this.$message.error('完善岗位经验')
        return
      }
      if (!this.editOption.age) {
        this.$message.error('年龄不能为空')
        return
      }
      if (!this.editOption.recruit_examine_user) {
        this.$message.error('选择简历审核人')
        return
      }
      if (!this.editOption.interview_user) {
        this.$message.error('选择面试考核人')
        return
      }
      if (!this.editOption.employ_user) {
        this.$message.error('选择录用审核人')
        return
      }
      // editOption.requirements  其他字段可保存在此
      // typeJob 1 全职 2 兼职 3 实习 4 不限 岗位性质 ；  credit 个人征信 1 需要 0 不需要
      // 岗位经验验证       starYear endYear
      // let  requirements = {};
      // let  dataOption = this.getJson(this.editOption)
      if (this.starYear && this.endYear) {
        dataOption.experience = `${this.starYear}-${this.endYear}`
      }
      dataOption.requirements = JSON.stringify(this.requirements)
      console.log('提交的数据招聘')
      // 判断新增 编辑
      let url_ = this.api.recruit.resume.add
      this.$post(url_, dataOption).then((response) => {
        if (!response.code) {
          if (type !== 'release') {
            this.$message.success(response.msg)
            this.$emit('InitFn')
            if (!this.resumeId) {
              this.clearFrom()
            }
          } else {
            // 保存成功后提交发布招聘
            this.adoptHttpFn(option)
          }
        } else {
          this.$message.error(response.msg)
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    // 获取详情
    async InitFn() {
      let data = await this.$fetch(this.api.recruit.resume.edit, {id: this.resumeId});
      console.log('data物料信息:', data)
      if (!data.code) {
        if (data.vo.experience) {
          let a = data.vo.experience.split('-')
          this.starYear = a[0]
          this.endYear = a[1]
        }
        this.examine_user_tag = [this.getJson(data.recruit_examine_user)]
        this.examine_user = this.getJson(data.recruit_examine_user);
        // employ_user  interview_user
        let interview_user = this.getJson(data.vo.interview_user);
        for (let i in interview_user) {
          this.dynamicTags_interview.push(interview_user[i])
          this.dynamicTagsId_interview.push(interview_user[i].id)
          this.dynamicTagsArray_interview[interview_user[i].id] = interview_user[i]
        }
        console.log('dynamicTagsId_interview:', this.dynamicTagsId_interview)

        let employ_user = this.getJson(data.vo.employ_user);
        for (let i in employ_user) {
          this.dynamicTags_employ.push(employ_user[i])
          this.dynamicTagsId_employ.push(employ_user[i].id)
          this.dynamicTagsArray_employ[employ_user[i].id] = employ_user[i]
        }
        console.log('dynamicTagsId_employ:', this.dynamicTagsId_employ)

        this.editOption.recruit_examine_user = this.examine_user.id
        this.editOption.interview_user = this.dynamicTagsId_interview.join(',');
        this.editOption.employ_user = this.dynamicTagsId_employ.join(',');

        this.requirements = this.getJson(data.vo.requirements)
        for (let i in this.editOption) {
          if (i !== 'recruit_examine_user' && i !== 'interview_user' && i !== 'employ_user' && i !== 'requirements') {
            if (data.vo[i]) {
              this.editOption[i] = data.vo[i]
            }
          }
        }

        console.log('编辑整理的数据：', this.editOption)
        // this.dynamicTags_employ = [];
        // this.dynamicTagsId_employ = [];
        // this.dynamicTagsArray_employ = {};
        // editOption: {
        //   job_id: '',// 	字符串 	必填 	1 	- 	招聘岗位id
        //       number: '',// 	字符串 	必填 	1 	- 	招聘人数
        //       treatment: '',// 	字符串 	必填 	1 	- 	待遇
        //       trial_time: '',//	字符串 	必填 	1 	- 	使用期限
        //       end_time: '',// 	字符串 	必填 	1 	- 	结束时间
        //       sex: 0,// 	字符串 	必填 	1 	- 	性别0不限1男2女
        //       age: '',// 	字符串 	必填 	1 	- 	年龄（以上）
        //       education: '',//	字符串 	必填 	1 	- 	学历id
        //       experience: '',//	字符串 	必填 	1 	- 	工作经验
        //       requirements: '',//	字符串 	必填 	1 	- 	岗位要求json
        //       recruit_examine_user: '',//	字符串 	必填 	1 	- 	简历审核人
        //       interview_user: '',//	字符串 	必填 	1 	- 	面试考核人员id多选逗号隔开
        //       employ_user: '',//	字符串 	必填 	1 	- 	录用考核人员id多选逗号隔开
        // }, // 编辑界面数据
        // starYear: '',
        //     endYear: '',
        //     typeJob: 1, // 工作性质
        //     credit: 1, // 征信
      } else {
        this.$message.error(data.msg)
      }
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
    clearFrom() {
      // editOption: {
      //   job_id: '',// 	字符串 	必填 	1 	- 	招聘岗位id
      //       number: '',// 	字符串 	必填 	1 	- 	招聘人数
      //       treatment: '',// 	字符串 	必填 	1 	- 	待遇
      //       trial_time: '',//	字符串 	必填 	1 	- 	使用期限
      //       end_time: '',// 	字符串 	必填 	1 	- 	结束时间
      //       sex: 0,// 	字符串 	必填 	1 	- 	性别0不限1男2女
      //       age: '',// 	字符串 	必填 	1 	- 	年龄（以上）
      //       education: '',//	字符串 	必填 	1 	- 	学历id
      //       experience: '',//	字符串 	必填 	1 	- 	工作经验
      //       requirements: '',//	字符串 	必填 	1 	- 	岗位要求json
      //       recruit_examine_user: '',//	字符串 	必填 	1 	- 	简历审核人
      //       interview_user: '',//	字符串 	必填 	1 	- 	面试考核人员id多选逗号隔开
      //       employ_user: '',//	字符串 	必填 	1 	- 	录用考核人员id多选逗号隔开
      // }, // 编辑界面数据
      for (let i in this.editOption) {
        if (i !== 'sex') {
          this.editOption[i] = ''
        }
      }
      this.typeJob = 1
      this.credit = 1
      this.starYear = ''
      this.endYear = ''

      this.examine_user_tag = []
      this.examine_user = {};

      this.dynamicTags_interview = [];
      this.dynamicTagsId_interview = [];
      this.dynamicTagsArray_interview = {};

      this.dynamicTags_employ = [];
      this.dynamicTagsId_employ = [];
      this.dynamicTagsArray_employ = {};
    },
    studentFn(option) {
      console.log('获取到选择的内容：', option, this.moreTeachType)
      // this.moreTeachType 选择的类型 1 简历审核 2 面试 3 录用
      if (this.moreTeachType === 1) {
        this.examine_user_tag = [];
        this.examine_user = {};
        this.examine_user_tag.push(option);
        this.examine_user = option;
        this.editOption.recruit_examine_user = option.id
      } else {
        if (this.moreTeachType === 2) {
          // 面试
          this.dynamicTags_interview = [];
          this.dynamicTagsId_interview = [];
          this.dynamicTagsArray_interview = {};
        } else {
          // 录用
          this.dynamicTags_employ = [];
          this.dynamicTagsId_employ = [];
          this.dynamicTagsArray_employ = {};
        }
        for (let i in option) {
          if (this.moreTeachType === 2) {
            this.dynamicTags_interview.push(option[i])
            this.dynamicTagsId_interview.push(option[i].id)
            this.dynamicTagsArray_interview = option
          } else {
            this.dynamicTags_employ.push(option[i])
            this.dynamicTagsId_employ.push(option[i].id)
            this.dynamicTagsArray_employ = option
          }
        }
      }
      if (this.moreTeachType === 2) {
        this.editOption.interview_user = this.dynamicTagsId_interview.join(',')
      }
      if (this.moreTeachType === 3) {
        this.editOption.employ_user = this.dynamicTagsId_employ.join(',')
      }
      console.log('提交的参数：', this.editOption)
    },
    handleClose(type, index) {
      // interview dynamicTags_interview; employ dynamicTags_employ
      if (type === 'interview') {
        this.dynamicTags_interview.splice(index, 1)
        delete this.dynamicTagsArray_interview[this.dynamicTagsId_interview[index]] // 去除选中的人
        this.dynamicTagsId_interview.splice(index, 1)
      } else {
        this.dynamicTags_employ.splice(index, 1)
        delete this.dynamicTagsArray_employ[this.dynamicTagsId_employ[index]] // 去除选中的人
        this.dynamicTagsId_employ.splice(index, 1)
      }
      // this.dynamicTags.splice(index, 1)
      // delete this.dynamicTagsArray_employ[this.dynamicTagsId[index]] // 去除选中的人
      // this.dynamicTagsId_employ.splice(index, 1)
      // this.infoForm.user_ids = this.dynamicTagsId.join(',')
    },
    handleCloseExamine() {
      this.examine_user = {}
    },
    // 打开教职工
    getMoreTeach(option) {
      console.log('打开选择内容：', this.moreTeachType, option)
      this.moreTeachType = option;
      if (option === 1) {
        this.singleTeachVisible = true;
      } else {
        if (this.moreTeachType === 2) {
          this.dynamicTags = this.getJson(this.dynamicTags_interview)
          this.dynamicTagsId = this.getJson(this.dynamicTagsId_interview)
          this.dynamicTagsArray = this.getJson(this.dynamicTagsArray_interview)
        } else {
          this.dynamicTags = this.getJson(this.dynamicTags_employ)
          this.dynamicTagsId = this.getJson(this.dynamicTagsId_employ)
          this.dynamicTagsArray = this.getJson(this.dynamicTagsArray_employ)
        }
        // this.moreTeachType===2?(this.dynamicTags = this.getJson(this.dynamicTags_interview),this.dynamicTagsId=this.getJson(this.dynamicTagsId_interview),this.dynamicTagsArray = this.getJson(this.dynamicTagsArray_interview)):(this.dynamicTags = this.getJson(this.dynamicTags_employ),this.dynamicTagsId=this.getJson(this.dynamicTagsId_employ),this.dynamicTagsArray = this.getJson(this.dynamicTagsArray_employ))
        this.moreTeachVisible = true;
      }
    },
    // 获取岗位信息
    async JobFn() {
      let data = await this.$fetch(this.api.recruit.job.index, {p: 1});
      this.jobList = data.list.data;
      console.log('岗位管理：', data)
    },
    // 获取岗位信息
    async EducationFn() {
      let data = await this.$fetch(this.api.recruit.education.index, {});
      this.educationList = data.list;
      console.log('岗位管理educationList：', data)
    },
    // 1 通过审核 0 驳回
    adoptFn(option) {
      let n = this;
      if (option === 1) {
        this.$confirm('确认发布本招聘信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // n.adoptHttpFn(option)
          // 此处需要进行提交编辑
          n.submitFromFn('release', option)
        }).catch(() => {

        });
      } else {
        this.$prompt('确认驳回本招聘信息, 填写驳回备注?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({value}) => {
          console.log(value)
          n.adoptHttpFn(option, value)
        }).catch(() => {
        });
      }
    },
    // 请求审核，驳回
    async adoptHttpFn(option, reason) {
      // 1 通过审核 0 驳回
      let dataOption = {id: this.resumeId}
      if (option !== 1) {
        dataOption.reason = reason
      }
      let data = await this.$fetch(option === 1 ? this.api.recruit.resume.adopt : this.api.recruit.resume.reject, dataOption);
      if (!data.code) {
        this.$message.success(data.msg)
        this.$emit('InitFn')
        this.closePOpup()
      } else {
        this.$message.error(data.msg)
      }
      console.log('岗位管理educationList：', data)
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 70%;
  height: 80%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 10px 20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.resruitment-title {
  padding: 5px 0;
  border-bottom: 2px solid #abd3f3;
  margin-bottom: 20px;
}

.el-radio {
  margin-right: 20px;
}

.jobTime {
  display: flex;
  align-items: center;
}

.jobTime span {
  padding: 0 10px;
}
</style>


